<template>
    <div>
        <van-nav-bar title="注册" left-arrow @click-left="onClickLeft"  />
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="phone" name="手机号" label="手机号" placeholder="手机号" :rules="[
                    { required: true, message: '请填写手机号' },
                    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' }
                ]" />
                <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码" :rules="[
                    { required: true, message: '请填写密码' },
                    { pattern: /^[a-zA-Z0-9]{6,12}$/, message: '密码格式不正确' }
                ]" />
                <van-field v-model="verify"  name="确认密码" label="确认密码" placeholder="确认密码" :rules="[
                    { required: true, message: '请填写密码' },
                    { pattern: /^[a-zA-Z0-9]{6,12}$/, message: '请确认密码' }
                ]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    注册
                </van-button>
            </div>
        </van-form>

    </div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import instance from '../axios/index.ts';
import { showToast } from 'vant';
const router = useRouter();
const onClickLeft = () => history.back();
const phone = ref('');
const password = ref('');
const verify = ref('');//确认密码
const onSubmit = () => {
    const data = {
        phone: phone.value,
        password: password.value,
    };
    instance.post('/api/register', data).then((res) => {
        if (password.value !== verify.value) {
            showToast('两次密码不一致');
            return;
        }
        if (res.data.code === 200) {
            showToast('注册成功');
            router.push('/home/home');
        } else {
            showToast('注册失败');
        }
    })
};
</script>
<style scoped></style>